package com.zzh.study.utils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用响应结果
 * 
 * @author caiwenhao
 * @date 2020年7月31日 下午12:19:15
 * @see [相关类/方法]
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeneralResult<T> {
    /**
     * 结果状态, true 成功 false 失败
     */
    private Boolean resultStatus;
    
    /**
     * 如果错误则返回 错误代码
     */
    private String errorCode;
    
    /**
     * 错误详情描述
     */
    private String errorMessage;
    
    /**
     * 结果为true时的 业务数据，可为空
     */
    private T resultData;
    
    public GeneralResult()
    {
    }
    
    public GeneralResult(ErrorCode error)
    {
        this(Boolean.FALSE, error.getCode(), error.getMsg());
    }
    
    public GeneralResult(ErrorCode error, T obj)
    {
        this(Boolean.FALSE, error.getCode(), error.getMsg());
        this.resultData = obj;
    }
    
    public GeneralResult(String errorCode, String errorMessage)
    {
        this(Boolean.FALSE, errorCode, errorMessage);
    }
    
    public GeneralResult(Boolean resultStatus, String errorCode, String errorMessage)
    {
        this.resultStatus = resultStatus;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }
    
    public static GeneralResult<Object> ok() {
        GeneralResult<Object> result = new GeneralResult<Object>();
        result.setResultStatus(Boolean.TRUE);
        return result;
    }
    
    public static GeneralResult<Object> error(ErrorCode errCode) {
        return new GeneralResult<Object>(Boolean.FALSE, errCode.getCode(), errCode.getMsg());
    }
    
    public static <T> GeneralResult<T> response(T resultData) {
        GeneralResult<T> result = new GeneralResult<T>();
        result.setResultStatus(Boolean.TRUE);
        result.setResultData(resultData);
        return result;
    }
    
    public static <T> GeneralResult<T> ok(T resultData) {
        GeneralResult<T> result = new GeneralResult<T>();
        result.setResultStatus(Boolean.TRUE);
        result.setResultData(resultData);
        return result;
    }
    
    public static GeneralResultMap responseMap() {
        return new GeneralResultMap();
    }
    
    public Boolean getResultStatus() {
        return resultStatus;
    }
    
    public String getErrorCode() {
        return errorCode;
    }
    
    public String getErrorMessage() {
        return errorMessage;
    }
    
    public T getResultData() {
        return resultData;
    }
    
    public void setResultStatus(Boolean resultStatus) {
        this.resultStatus = resultStatus;
    }
    
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
    
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
    
    public void setResultData(T resultData) {
        this.resultData = resultData;
    }
    
    public void setErr(ErrorCode errCode) {
        this.errorCode = errCode.getCode();
        this.errorMessage = errCode.getMsg();
        this.resultStatus = false;
    }
    
    /**
     * 通用的Map响应结果
     * 
     * @author caiwenhao
     * @date 2020年7月31日 下午12:21:46
     * @see [相关类/方法]
     */
    public static class GeneralResultMap {
        
        private Map<String, Object> resMap = new HashMap<String, Object>();
        
        public GeneralResultMap addParam(String key, Object value) {
            this.resMap.put(key, value);
            return this;
        }
        
        public GeneralResultMap addParams(Map<String, Object> params) {
            this.resMap.putAll(params);
            return this;
        }
        
        public GeneralResult<Map<String, Object>> build() {
            GeneralResult<Map<String, Object>> result = new GeneralResult<Map<String, Object>>();
            result.setResultStatus(Boolean.TRUE);
            result.setResultData(this.resMap);
            return result;
        }
    }
    
    @Override
    public String toString() {
        return "GeneralResult [resultStatus=" + resultStatus + ", errorCode=" + errorCode + ", errorMessage="
            + errorMessage + ", resultData=" + resultData + "]";
    }
    
}
